package com.sz.mes.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.sz.mes.entity.warehouse.Area;
import com.sz.mes.entity.warehouse.Lattice;
import com.sz.mes.entity.warehouse.LatticeGoods;
import com.sz.mes.entity.warehouse.Shelves;
import com.sz.mes.mapper.warehouse.AreaMapper;
import com.sz.mes.mapper.warehouse.LatticeGoodsMapper;
import com.sz.mes.mapper.warehouse.LatticeMapper;
import com.sz.mes.mapper.warehouse.ShelvesMapper;
import com.sz.mes.service.warehouse.AreaService;
import com.sz.mes.service.impl.BaseCustomServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓库货区相关Service实现
 *
 * @author hem
 * @version 1.0
 * @date 2023-05-25
 * Copyright © szhs
 */
@Service
@Slf4j
public class AreaServiceImpl extends BaseCustomServiceImpl<AreaMapper, Area> implements AreaService {
    private AreaMapper areaMapper;
    private ShelvesMapper shelvesMapper;
    private LatticeMapper latticeMapper;
    private LatticeGoodsMapper latticeGoodsMapper;

    public AreaServiceImpl(AreaMapper areaMapper, ShelvesMapper shelvesMapper, LatticeMapper latticeMapper,
                           LatticeGoodsMapper latticeGoodsMapper) {
        this.areaMapper = areaMapper;
        this.shelvesMapper = shelvesMapper;
        this.latticeMapper = latticeMapper;
        this.latticeGoodsMapper = latticeGoodsMapper;
    }

    @Override
    public JsonResult deleteById(Long id) {
        QueryWrapper<Shelves> shelvesQueryWrapper = new QueryWrapper<>();
        shelvesQueryWrapper.eq("warehouse_area_id", id);
        List<Shelves> shelvesList = shelvesMapper.selectList(shelvesQueryWrapper);
        for (Shelves shelves : shelvesList) {
            Long shelvesId = shelves.getId();
            QueryWrapper<Lattice> latticeQueryWrapper = new QueryWrapper<>();
            latticeQueryWrapper.eq("warehouse_shelves_id", shelvesId);
            List<Lattice> latticeList = latticeMapper.selectList(latticeQueryWrapper);
            //查询货架上是否存在商品
            for (Lattice lattice : latticeList) {
                QueryWrapper<LatticeGoods> latticeGoodsQueryWrapper = new QueryWrapper<>();
                latticeGoodsQueryWrapper.eq("warehouse_lattice_id", lattice.getId());
                //一个货架可能有多个商品，所以用selectlist
                List<LatticeGoods> latticeGoodsList = latticeGoodsMapper.selectList(latticeGoodsQueryWrapper);
                if (latticeGoodsList.size() > 0) {
                    //如果该货架存在商品则，不能删除
                    return JsonResult.FAIL_EXCEPTION("货架上还存在商品，请先处理商品");
                }else {
                    Integer lattices = latticeMapper.deleteByLatticeId(lattice.getId());
                    Integer shelve = shelvesMapper.deleteByShelveId(shelvesId);
                }
            }
        }
        //如果能从for走下来，证明货架上全都没有商品，可以删除所以货架
        Integer area = areaMapper.deleteByAreaId(id);
        if (area > 0) {
            return JsonResult.OK("删除成功");
        } else {
            return JsonResult.FAIL_EXCEPTION("删除失败");
        }
    }
}
